﻿#region

using System;
using System.Collections.Generic;
using MediaSage.Lib.Utilities;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace MediaSage.Lib.Database
{
    public class PersonEntity : DatabaseObject
    {
        #region Constructors

        public PersonEntity()
        {
            Filmography = new List<FilmographyEntity>();
            Job = new List<string>();
            Nominations = new List<string>();
            Images = new List<ImageEntity>();
            AlsoKnownAs = new List<string>();
        }

        #endregion Constructors

        #region Properties

        public virtual IList<string> AlsoKnownAs { get; set; }

        public virtual string Biography { get; set; }

        public virtual DateTime? Birthday { get; set; }

        public virtual string BirthPlace { get; set; }

        public virtual DateTime? Death { get; set; }

        public virtual string Department { get; set; }

        public virtual IList<FilmographyEntity> Filmography { get; set; }

        public virtual Guid ID { get; set; }

        public virtual ImageEntity Image { get; set; }

        public virtual IList<ImageEntity> Images { get; set; }

        public virtual string IMDBID { get; set; }

        public virtual IList<string> Job { get; set; }

        public virtual string Name { get; set; }

        public virtual IList<string> Nominations { get; set; }

        public virtual string ThumbURL { get; set; }

        public virtual int TMDBID { get; set; }

        public virtual int TVDBID { get; set; }

        public virtual string URL { get; set; }

        #endregion Properties

        public override void Save()
        {
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                PersonEntity dbCE = null;
                if (config.MergeOnPersonName)
                {
                    Disjunction disj = Restrictions.Disjunction();
                    disj.Add(Restrictions.Eq("TMDBID", TMDBID));
                    disj.Add(Restrictions.Eq("IMDBID", IMDBID));
                    disj.Add(Restrictions.InsensitiveLike("Name", Name));
                    dbCE = session.CreateCriteria(typeof(PersonEntity)).Add(disj).UniqueResult<PersonEntity>();
                }
                else
                {
                    Disjunction disj = Restrictions.Disjunction();
                    disj.Add(Restrictions.Eq("TMDBID", TMDBID));
                    disj.Add(Restrictions.Eq("IMDBID", IMDBID));
                    dbCE = session.CreateCriteria(typeof(PersonEntity)).Add(disj).UniqueResult<PersonEntity>();
                }
                if (dbCE != null)
                {
                    PersonEntity merged = MediaUtil.MergePersonEntity(this, dbCE, config.MergeType,
                                                                      config.MergeOnPersonName);
                    AlsoKnownAs = merged.AlsoKnownAs;
                    Biography = merged.Biography;
                    Birthday = merged.Birthday;
                    BirthPlace = merged.BirthPlace;
                    Death = merged.Death;
                    Department = merged.Department;
                    Filmography = merged.Filmography;
                    ID = dbCE.ID;
                    Image = merged.Image;
                    Images = merged.Images;
                    IMDBID = merged.IMDBID;
                    Job = merged.Job;
                    Name = merged.Name;
                    Nominations = merged.Nominations;
                    ThumbURL = merged.ThumbURL;
                    TMDBID = merged.TMDBID;
                    URL = merged.URL;
                }
            }
            base.Save();
        }

        public override void SaveOrUpdate()
        {
            ISession session = DBUtil.GetSession();
            using (session.BeginTransaction())
            {
                PersonEntity dbCE = null;
                if (config.MergeOnPersonName)
                {
                    Disjunction disj = Restrictions.Disjunction();
                    disj.Add(Restrictions.Eq("TMDBID", TMDBID));
                    disj.Add(Restrictions.Eq("IMDBID", IMDBID));
                    disj.Add(Restrictions.InsensitiveLike("Name", Name));
                    dbCE = session.CreateCriteria(typeof(PersonEntity)).Add(disj).UniqueResult<PersonEntity>();
                }
                else
                {
                    Disjunction disj = Restrictions.Disjunction();
                    disj.Add(Restrictions.Eq("TMDBID", TMDBID));
                    disj.Add(Restrictions.Eq("IMDBID", IMDBID));
                    dbCE = session.CreateCriteria(typeof(PersonEntity)).Add(disj).UniqueResult<PersonEntity>();
                }
                if (dbCE != null)
                {
                    PersonEntity merged = MediaUtil.MergePersonEntity(this, dbCE, config.MergeType,
                                                                      config.MergeOnPersonName);
                    AlsoKnownAs = merged.AlsoKnownAs;
                    Biography = merged.Biography;
                    Birthday = merged.Birthday;
                    BirthPlace = merged.BirthPlace;
                    Death = merged.Death;
                    Department = merged.Department;
                    Filmography = merged.Filmography;
                    ID = dbCE.ID;
                    Image = merged.Image;
                    Images = merged.Images;
                    IMDBID = merged.IMDBID;
                    Job = merged.Job;
                    Name = merged.Name;
                    Nominations = merged.Nominations;
                    ThumbURL = merged.ThumbURL;
                    TMDBID = merged.TMDBID;
                    URL = merged.URL;
                }
            }
            base.SaveOrUpdate();
        }
    }
}